/*
 * @Description: 导入所有 vuex 模块，自动加入namespaced:true，用于解决vuex命名冲突
 * @Version: 1.0
 * @Autor: wangqiang
 * @Date: 2021-06-25 09:45:07
 * @LastEditors: wangqiang
 * @LastEditTime: 2021-07-01 10:32:31
 */
import { InjectionKey } from 'vue'

import { createStore, useStore as baseUseStore, Store } from 'vuex';

import { RootStateTypes } from '@/store/interface';

// 导入所有 vuex 模块
const files = require.context('./modules', false, /\.ts$/)
const modules: any = {}
files.keys().forEach((key) => {
  modules[key.replace(/(\.\/|\.ts)/g, '')] = files(key).default
})
Object.keys(modules).forEach((key) => {
  modules[key]['namespaced'] = true
})

console.log(modules)

export const key: InjectionKey<Store<RootStateTypes>> = Symbol();

export const store = createStore<RootStateTypes>({ modules });

export function useStore() {
	return baseUseStore(key);
}
